<?php

/**
 * Request handling module file
 *
 * @author coder_
 */

class Request extends _Module
{
    private static $instance;
    private $dependencies = array('Url');

    private $handlersFile;
    private $filtersFile;
    private $handlers;
    private $filters;
    private $uri;
    private $ControllerMapping;
    private $FilterMapping;
    private $request_method;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    public static function &getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function initialize()
    {
        $path = CONFIGS . '/handlers.xml';
        if(file_exists($path))
        {
            $this->handlersFile = $path;
        }

        $path = CONFIGS . '/filters.xml';
        if(file_exists($path))
        {
            $this->filtersFile = $path;
        }

        $this->uri = getModule('Url')->getUri();

        $this->request_method = $_SERVER['REQUEST_METHOD'];

        $this->loadConfigurations();

        $this->loadMappings();
    }

    public function getDependencies()
    {
        return $this->dependencies;
    }

    public function findController()
    {
        $keys = array_keys($this->ControllerMapping);

        foreach($keys as $key)
        {
            $k = str_replace('/', '\/', $key);
            $k = str_replace('-', '\-', $k);
            $regex = '/^' . $k . '$/i';
            $mc = preg_match($regex, $this->uri);
            if($mc)
            {
                $controller = $this->ControllerMapping[$key];
                break;
            }
        }

        if(isset($controller)) return strtolower((string) $controller->name);
        else return '';
    }

    public function getRequestMethod()
    {
        return (string) $this->request_method;
    }

    public function getParameters()
    {
        return $_REQUEST;
    }

    public function getParameter($name)
    {
        if(isset($_REQUEST[$name])) return $_REQUEST[$name];
        else return null;
    }

    private function loadConfigurations()
    {
        if($this->handlersFile) $this->handlers = simplexml_load_file($this->handlersFile);
        if($this->filtersFile) $this->filters = simplexml_load_file($this->filtersFile);
    }

    private function loadMappings()
    {
        foreach($this->handlers->controller as $controller)
        {
            $this->ControllerMapping[(string)$controller->uri] = $controller;
        }

        $this->FilterMapping['pre'] = array();
        $this->FilterMapping['post'] = array();

        foreach($this->filters->filter as $filter)
        {
            switch($filter->type)
            {
                case 'pre':
                    if(!isset($this->FilterMapping['pre'][(string)$filter->uri]))
                        $this->FilterMapping['pre'][(string)$filter->uri] = array();
                    array_push($this->FilterMapping['pre'][(string)$filter->uri], $filter);
                    break;
                case 'post':
                    if(!isset($this->FilterMapping['post'][(string)$filter->uri]))
                        $this->FilterMapping['post'][(string)$filter->uri] = array();
                    array_push($this->FilterMapping['post'][(string)$filter->uri], $filter);
                    break;
            }
        }
    }
}